package com.magicsoft.share.ui.main;

import android.content.Intent;
import android.graphics.Paint;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.magicsoft.share.R;
import com.magicsoft.share.base.BaseActivity;
import com.magicsoft.share.bean.main.User;
import com.magicsoft.share.constant.AppDavikActivityMgr;
import com.magicsoft.share.constant.SPKey;
import com.magicsoft.share.constant.Url;
import com.magicsoft.share.customview.alertview.AlertView;
import com.magicsoft.share.presenter.main.LoginPresenter;
import com.magicsoft.share.utils.AlerterUtils;
import com.magicsoft.share.utils.SPUtils;
import com.magicsoft.share.utils.Validator;
import com.magicsoft.share.view.main.ILoginView;

import butterknife.BindView;
import butterknife.OnClick;

public class LoginActivity extends BaseActivity<ILoginView, LoginPresenter> implements ILoginView {

    @BindView(R.id.iv_logo)
    ImageView ivLogo;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.textView)
    TextView textView;
    @BindView(R.id.tv_clause)
    TextView tvClause;
    @BindView(R.id.tv_mobile)
    TextView tvMobile;
    @BindView(R.id.edt_mobile)
    EditText edtMobile;
    @BindView(R.id.tv_password)
    TextView tvPassword;
    @BindView(R.id.edt_password)
    EditText edtPassword;
    @BindView(R.id.tv_forget_password)
    TextView tvForgetPassword;
    @BindView(R.id.btn_login)
    Button btnLogin;
    @BindView(R.id.tv_register)
    TextView tvRegister;
    private String mobileStr;
    private String passWordStr;

    @Override
    public LoginPresenter initPresenter() {
        return new LoginPresenter();
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    public void initView() {
        tvRegister.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
    }

    @Override
    public void initData() {

    }

    @Override
    public void requestData() {

    }

    @OnClick({R.id.iv_back, R.id.tv_clause, R.id.tv_forget_password, R.id.btn_login, R.id.tv_register})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_clause:
                //服务条款
                Intent intent1 = new Intent(this, H5Activity.class);
                intent1.putExtra("urlStr", Url.BASE_URL + "api/open/platform/termsOfService");
                intent1.putExtra("titleStr", "服务条款");
                startActivity(intent1);
                break;
            case R.id.tv_forget_password:
                //忘记密码
                startActivity(new Intent(this, InputMobileActivity.class));
                break;
            case R.id.btn_login:
                //登录
                mobileStr = edtMobile.getText().toString();
                passWordStr = edtPassword.getText().toString();
                if (TextUtils.isEmpty(mobileStr)) {
                    AlerterUtils.warning(this, "请输入手机号");
                } else if (TextUtils.isEmpty(passWordStr)) {
                    AlerterUtils.warning(this, "请输入密码");
                } else if (!Validator.isMobile(mobileStr)) {
                    AlerterUtils.warning(this, "请输入正确的手机号");
                } else {
                    login();
                }
                break;
            case R.id.tv_register:
                //注册
                startActivity(new Intent(this, RegisterActivity.class));
                break;
            default:
                break;
        }
    }

    /**
     * 登录
     */


    @Override
    public void loginSuccess(User user) {
        saveUserIfo(user);
        AppDavikActivityMgr.getScreenManager().finishActivity(MainActivity.class);
        startActivity(new Intent(this, MainActivity.class));
        finish();
    }


    @Override
    public void loginFail(String msg) {
        new AlertView.Builder().setContext(this)
                .setStyle(AlertView.Style.Alert)
                .setTitle("登录失败")
                .setMessage(msg)
                .setCancelText("确定")
                .build()
                .show();
    }

    private void login() {
        mPresenter.login(mobileStr, passWordStr);

    }

    private void saveUserIfo(User user) {
        SPUtils.getInstance().put(SPKey.ID, user.getId());
        SPUtils.getInstance().put(SPKey.TOKEN, user.getToken());
        SPUtils.getInstance().put(SPKey.MOBILE, user.getPhone());
    }
}
